package com.csy.repair.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.csy.commons.dto.SysUser;
import com.csy.feign.user.api.UserServiceApi;
import com.csy.repair.entity.AjRepair;
import com.csy.repair.mapper.AjRepairMapper;
import com.csy.repair.service.AjRepairService;

import lombok.extern.slf4j.Slf4j;

/**
 *
 */
@Service
@Slf4j
public class AjRepairServiceImpl implements AjRepairService {

    private final String PREFIX_CODE = "BX_";
    @Resource
    private AjRepairMapper ajRepairMapper;
    @Resource
    private UserServiceApi userServiceApi;
    @Override
    public List<AjRepair> selectRepairList(AjRepair ajRepair) {
        return ajRepairMapper.selectRepairList(ajRepair);
    }

    @Override
    public AjRepair selectRepairById(Long repairId) {
        return ajRepairMapper.selectRepairById(repairId);
    }

    @Override
    public int countRepair() {
        return ajRepairMapper.countRepair();
    }

    @Override
    public int add(AjRepair ajRepair) throws Exception {
        ajRepair.setRepairNum(PREFIX_CODE+System.currentTimeMillis());
        String userId = ajRepair.getUserId();
        
    	// 调用csy-user服务的接口
        SysUser user = userServiceApi.getByName(userId);
        if (null == null) {
        	throw new Exception("查询用户失败: userId=" + userId);
        }

        ajRepair.setUserId(user.getUserId().toString());

        return ajRepairMapper.insert(ajRepair);
    }

    @Override
    public int delByIds(List<Long> ids) {
        return ajRepairMapper.delByIds(ids);
    }
}
